package com.atomos.strategy.initialize.position
{
	import com.atomos.utils.PointUtils;	
	
	import flash.geom.Point;
	
	import com.atomos.particle.Particle;
	import com.atomos.strategy.initialize.InitializeStrategy;
	import com.atomos.utils.RandomUtils;
	import com.bourre.error.NullPointerException;	

	public class LineInitializer implements InitializeStrategy
	{
		protected var _pA : Point;
		protected var _pB : Point;
		protected var _pSegment : Point;
		protected var _nRandom : Number;
		
		public function LineInitializer ( a : Point, b : Point, rand : Number = 0 )
		{
			_pA = a;
			_pB = b;
			_nRandom = isNaN ( rand ) ? 0 : rand;
			
			try 
			{ 
				_pSegment = _pB.clone();
				_pSegment.subtract( _pA ); 
			}
			catch ( e : Error )
			{
				throw new NullPointerException ( "One of the points passed to " + this + 
												 "is null : A=" + _pA + ", B=" + _pB );
			}
		}
		
		public function initialize( particle : Particle ) : void
		{
			var p : Point = _pSegment.clone( );
			PointUtils.scale( p, Math.random() );
			
			particle.lastPosition.x = particle.position.x = _pA.x + p.x + RandomUtils.balance( _nRandom );
			particle.lastPosition.y = particle.position.y = _pA.y + p.y + RandomUtils.balance( _nRandom );
		}
		
	}
}